package cn.fanyamin.business;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.fanyamin.dao.BookDao;
import cn.fanyamin.dao.UserDao;
import cn.fanyamin.domain.Book;
import cn.fanyamin.domain.BookSearchCriteria;
import cn.fanyamin.domain.BorrowLog;
import cn.fanyamin.domain.User;
import cn.fanyamin.web.vo.BookVO;

/**
 * @author walter
 *
 */
public class BookServiceImpl implements BookService {
	private static Log logger = LogFactory.getLog(BookServiceImpl.class);
	private BookDao bookDao;
	private UserDao userDao;
	
	public void setBookDao(BookDao bookDao) {
		this.bookDao = bookDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public int createBook(BookVO vo) {
		Book book = vo.getBook();
		int bookID = bookDao.createBook(book);
		BorrowLog bl = vo.getBorrowLog();
		if(bl != null) {
			String userName = vo.getBorrowerName();
			if(StringUtils.isNotBlank(userName)) {
				User user = userDao.findUserByName(userName);
				if(null == user) {
					throw new RuntimeException("Cannot find the user");
				}
				bl.setBorrowerID(user.getUserID());
				bl.setBookID(bookID);
				int borrowLogID = bookDao.createBorrowLog(bl);
				book.setBookID(bookID);
				book.setBorrowLogID(borrowLogID);
				bookDao.updateBook(book);
			}
			
		}
		
		return bookID;
	}

	public int deleteBook(int id) {
		int ret = bookDao.deleteBorrowLogs(id);
		ret = bookDao.deleteBook(id);
		return ret;
	}

	public List<BookVO> listBook(int pageNo, int pageSize, BookSearchCriteria criteria) {		
		 return bookDao.listBook(pageNo, pageSize, criteria);
	}

	public List<BookVO> getUserBooks(int userID) {
		return bookDao.getUserBooks(userID);
	}

	public List<BookVO> getAllBooks() {
		return bookDao.getAllBooks();
	}
	
	public BookVO retrieveBook(int id) {
		BookVO vo = new BookVO();
		Book book = bookDao.retrieveBook(id);
		vo.setBook(book);
		int borrowLogID = book.getBorrowLogID();
		if(borrowLogID == 0 ) {
			return vo;
		}
		BorrowLog borrowLog = bookDao.retrieveBorrowLog(borrowLogID);
		if(null != borrowLog) {
			User user = userDao.retrieve(borrowLog.getBorrowerID());
			vo.setBorrowerName(user.getUserName());
			vo.setBorrowLog(borrowLog);
		}
		return vo;
	}

	public int updateBook(BookVO vo) {
		Book book = vo.getBook();
		int ret = 0;
		String userName = vo.getBorrowerName();
		if(StringUtils.isBlank(userName)) {
			//no one borrow the book
			book.setBorrowLogID(0);			
		} else {
			BorrowLog bl = vo.getBorrowLog();
			if(bl != null) {
				User user = userDao.findUserByName(userName);
				if(null == user) {
					throw new RuntimeException("Cannot find the user: " + userName);
				}
				bl.setBorrowLogID(book.getBorrowLogID());
				bl.setBookID(book.getBookID());
				bl.setBorrowerID(user.getUserID());
				logger.debug("save borrowLog:" + bl);
				if(bl.getBorrowLogID() > 0) {
					ret = bookDao.updateBorrowLog(bl);
				} else {
					int blID = bookDao.createBorrowLog(bl);
					book.setBorrowLogID(blID);
				}
			}
		}
				
		ret = bookDao.updateBook(book);
		return ret;
	}

	public int getBookCount(BookSearchCriteria criteria) {
		return bookDao.getBookCount(criteria);
	}

}
